package com.shcd.service;

import com.shcd.entity.ResultSelectMsg;
import com.shcd.mapperImpl.DataLifeCycleMapperImpl;
import com.shcd.tools.PageTools;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataLifeCycleService {

    private final DataLifeCycleMapperImpl dataLifeCycleMapperImpl;

    @Autowired
    public DataLifeCycleService(DataLifeCycleMapperImpl dataLifeCycleMapperImpl) {
        this.dataLifeCycleMapperImpl = dataLifeCycleMapperImpl;
    }

    public void InsertMain(String day) {
        List<Map<String, String>> cidList = dataLifeCycleMapperImpl.SelectCids();
        Map<String, String> m = new HashMap<>();
        for (Map<String, String> cid : cidList) {
            m.put("cid", cid.get("CID"));
            m.put("tableName", cid.get("TABNAME"));
            m.put("day", day);
            try {
                List<Map<String, String>> data = dataLifeCycleMapperImpl.SelectDescribe(m);
                if (data.size() != 0) {
                    dataLifeCycleMapperImpl.InsertData(data);
                }
            } catch (Exception e) {
                System.out.println("查询表" + cid.get("TABNAME") + "时出错");
            }
        }
    }

    public String SelectSum_LOG_EXCHANGE(net.sf.json.JSONObject o) {
        List<Map> res = new ArrayList<>();
        Map<String, Object> m = PageTools.getPageParaMap(o);
        String success = "true";
        String error = "查询成功";
        int resSize = 0;
        try {
            m.put("startTime", o.get("startTime"));
            m.put("endTime", o.get("endTime"));
            res = dataLifeCycleMapperImpl.SelectSum_LOG_EXCHANGE(m);
            resSize = dataLifeCycleMapperImpl.SelectSize_Sum_LOG_EXCHANGE(m).size();
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.out.println(e);
        }
        return getMsg(resSize, success, res, error, o.get("PAGESIZE").toString());
    }

    public String SelectLOG_EXCHANGEbyInserttime(net.sf.json.JSONObject o) {
        List<Map> res = new ArrayList<>();
        Map<String, Object> m = PageTools.getPageParaMap(o);
        String success = "true";
        String error = "查询成功";
        int resSize = 0;
        try {
            m.put("time", o.get("time"));
            m.put("unitId", o.get("unitId"));
            m.put("name", o.get("name"));
            res = dataLifeCycleMapperImpl.SelectLOG_EXCHANGEbyInserttime(m);
            resSize = dataLifeCycleMapperImpl.SelectSize_LOG_EXCHANGEbyInserttime(m).size();
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.out.println(e);
        }
        return getMsg(resSize, success, res, error, o.get("PAGESIZE").toString());
    }

    public String SelectDescribe_LOG_EXCHANGE(net.sf.json.JSONObject o) {
        List<Map> res = new ArrayList<>();
        Map<String, Object> m = PageTools.getPageParaMap(o);
        String success = "true";
        String error = "查询成功";
        int resSize = 0;
        try {
            m.put("time", o.get("time"));
            m.put("tablename", o.get("tablename"));
            m.put("where", SwitchWhere(o.get("sign").toString()));
            res = dataLifeCycleMapperImpl.SelectDescribe_LOG_EXCHANGE(m);
            resSize = dataLifeCycleMapperImpl.SelectSize_Describe_LOG_EXCHANGE(m).size();
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.out.println(e);
        }
        return getMsg(resSize, success, res, error, o.get("PAGESIZE").toString());
    }

    public void Insert_queryLogList(net.sf.json.JSONObject o) {
        Map<String, Object> m = new HashMap<>();
        m.put("dateStates", o.get("dateStates"));
        try {
            dataLifeCycleMapperImpl.Delete_queryLogList();
            List<Map> data = dataLifeCycleMapperImpl.Select_queryLogList(m);
            if (data.size() != 0) {
                dataLifeCycleMapperImpl.Insert_queryLogList(data);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public String queryLogList(net.sf.json.JSONObject o) {
        List<Map> res = new ArrayList<>();
        Map<String, Object> m = new HashMap<>();
        String success = "true";
        String error = "查询成功";
        try {
            if (o.get("dateStates").equals("-1"))
                m.put("where", " 1=1 ");
            else if (o.get("dateStates").equals("0"))
                m.put("where", " KETTLE_STATES='0' and ZJ_STATES='0' and GJ_STATES='0' ");
            else if (o.get("dateStates").equals("1"))
                m.put("where", " KETTLE_STATES='1' and ZJ_STATES='1' and GJ_STATES='1' ");
            else if (o.get("dateStates").equals("2"))
                m.put("where", " KETTLE_STATES='2' or ZJ_STATES='2' or GJ_STATES='2' ");
            else
                m.put("where", "1=2");
            m.put("unitCode", o.get("unitCode"));
            m.put("catalogName", o.get("catalogName"));
            res = dataLifeCycleMapperImpl.queryLogList(m);
        } catch (Exception e) {
            success = "false";
            error = "查询失败！";
            System.out.println(e);
        }
        return getMsg(res.size(), success, res, error, "1");
    }

    private String SwitchWhere(String sign) {
        switch (sign) {
            case "ZJTG_W":
                return "substr(mark_zjbs, 1, 1) = '1'";
            case "ZJTG_S":
                return "substr(mark_zjbs, 2, 1) = '1'";
            case "ZJBTG_W":
                return "substr(mark_zjbs, 1, 1) = '2'";
            case "GJ_COUNT_S":
                return "substr(mark_rkbs,4,1) = '1'";
            case "ZJBTG_S":
                return "substr(mark_zjbs,2,1) = '2'";
            case "WZJS_S":
                return "substr(mark_zjbs,2,1) = '0' and substr(mark_zjbs,1,1) = '1'";
            case "WZJS_W":
                return "substr(mark_zjbs, 1, 1) = '0'";
            case "GJ_COUNT_ZW":
                return "substr(mark_rkbs, 1, 1) = '1'";
            case "GJ_COUNT_DM":
                return "substr(mark_rkbs, 3, 1) = '1'";
            case "GJ_COUNT_DA":
                return "substr(mark_rkbs, 5, 1) = '1'";
            default:
                return "1=1";
        }
    }


    private String getMsg(int size, String success, List<Map> res, String error, String pageSize) {
        ResultSelectMsg resultSelectMsg = new ResultSelectMsg();
        resultSelectMsg.setTotalRowCount(size);
        resultSelectMsg.setSuccess(success);
        resultSelectMsg.setRows(res);
        resultSelectMsg.setError(error);
        resultSelectMsg.setPageSize(Integer.parseInt(pageSize));
        JSONObject result = new JSONObject(resultSelectMsg.getMsg());
        return result.toString();
    }
}
